package com.example.weli.ble2test;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;

public class DeviceListActivity extends Activity {
    private static final boolean D = true;
    public static String EXTRA_DEVICE_ADDRESS = "device_address";
    private static final String TAG = "DeviceListActivity";
    private BluetoothAdapter mBtAdapter;
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
            DeviceListActivity.this.mBtAdapter.cancelDiscovery();
            String info = ((TextView) v).getText().toString();
            if (info.length() >= 17) {
                String address = info.substring(info.length() - 17);
                Intent intent = new Intent();
                intent.putExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS, address);
                DeviceListActivity.this.setResult(-1, intent);
                DeviceListActivity.this.finish();
            }
        }
    };
    private ArrayAdapter<String> mNewDevicesArrayAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.bluetooth.device.action.FOUND".equals(action)) {
                BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                if (DeviceListActivity.this.mNewDevicesArrayAdapter.getPosition(device.getName() + "\n" + device.getAddress()) == -1) {
                    DeviceListActivity.this.mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            } else if ("android.bluetooth.adapter.action.DISCOVERY_FINISHED".equals(action)) {
                DeviceListActivity.this.setProgressBarIndeterminateVisibility(false);
                DeviceListActivity.this.setTitle(R.string.select_device);
                if (DeviceListActivity.this.mNewDevicesArrayAdapter.getCount() == 0) {
                    DeviceListActivity.this.mNewDevicesArrayAdapter.add(DeviceListActivity.this.getResources().getText(R.string.none_found).toString());
                }
                DeviceListActivity.this.findViewById(R.id.button_scan).setVisibility(View.VISIBLE);
            }
        }
    };
    private ListView newDevicesListView;
    private ListView pairedListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(5);
        setContentView(R.layout.devicelist);
        setResult(0);
        ((Button) findViewById(R.id.button_scan)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                DeviceListActivity.this.doDiscovery();
                v.setVisibility(View.GONE);
                DeviceListActivity.this.pairedListView.setVisibility(View.GONE);
            }
        });
        this.mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name);
        this.mNewDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name);
        this.pairedListView = (ListView) findViewById(R.id.paired_devices);
        this.pairedListView.setAdapter(this.mPairedDevicesArrayAdapter);
        this.pairedListView.setOnItemClickListener(this.mDeviceClickListener);
        this.newDevicesListView = (ListView) findViewById(R.id.new_devices);
        this.newDevicesListView.setAdapter(this.mNewDevicesArrayAdapter);
        this.newDevicesListView.setOnItemClickListener(this.mDeviceClickListener);
        registerReceiver(this.mReceiver, new IntentFilter("android.bluetooth.device.action.FOUND"));
        registerReceiver(this.mReceiver, new IntentFilter("android.bluetooth.adapter.action.DISCOVERY_FINISHED"));
        this.mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = this.mBtAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice device : pairedDevices) {
                this.mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        } else {
            this.mPairedDevicesArrayAdapter.add(getResources().getText(R.string.none_paired).toString());
        }
        if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") != 0) {
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.ACCESS_FINE_LOCATION"}, 1);
        } else if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION") != 0) {
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 2);
        }
    }

    @SuppressLint("WrongConstant")
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case joystick.COORDINATE_DIFFERENTIAL /*1*/:
                if (grantResults.length <= 0 || grantResults[0] != 0) {
                    Toast.makeText(this, "app\u6ca1\u6709\u5b9a\u4f4d\u6743\u9650\u5c06\u65e0\u6cd5\u641c\u7d22\u84dd\u7259\u8bbe\u5907!", 1);
                    return;
                }
                return;
            case UIActivity.xconnected /*2*/:
                if (grantResults.length <= 0 || grantResults[0] != 0) {
                    Toast.makeText(this, "app\u6ca1\u6709\u5b9a\u4f4d\u6743\u9650\u5c06\u65e0\u6cd5\u641c\u7d22\u84dd\u7259\u8bbe\u5907!", 1);
                    return;
                }
                return;
            default:
                return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.mBtAdapter != null) {
            this.mBtAdapter.cancelDiscovery();
        }
        unregisterReceiver(this.mReceiver);
    }

    private void doDiscovery() {
        Log.d(TAG, "doDiscovery()");
        setProgressBarIndeterminateVisibility(D);
        setTitle(R.string.scanning);
        findViewById(R.id.title_paired_devices).setVisibility(View.GONE);
        findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
        if (this.mBtAdapter.isDiscovering()) {
            this.mBtAdapter.cancelDiscovery();
        }
        this.mNewDevicesArrayAdapter.clear();
        this.mNewDevicesArrayAdapter.notifyDataSetChanged();
        this.mBtAdapter.startDiscovery();
    }
}
